package com.kdtech.analyse.Bbs;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.Bbs.common.BbsAnalyseDiscuz;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.AbstractMeta;
import com.kdtech.entity.data.NewsMeta;

public class XiciBbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		//http://www.xici.net/d85582998.htm
		String[] regex = {
							"http://www.xici.net/[a-z]*[0-9]*.htm",
							"http://www.xici.net/[a-z]*[0-9]*.htm[?]cmsid=[0-9]+"
							};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
			if (!isDetailPage(urlMeta.getUrl())) {
			}
			NewsMeta bbs = new NewsMeta();
			if (urlMeta.getHtml() == null) {

			}
			String htmltxt = urlMeta.getHtml();
			String url = urlMeta.getUrl();

			bbs.setUrl(url);
			String title = null;
			String content = null;
			Long date = null;
			Integer clickNum = 0;
			Integer commentNum = 0;
			String author=null;
			Document doc = Jsoup.parse(htmltxt);
			String jsonData = StringUtils.substringBetween(htmltxt, "var docData = ","function PreNextPageFunc(e)");
			if (jsonData!=null){
				int lastIndexOf = jsonData.lastIndexOf(";");
				if(lastIndexOf!=-1){
					jsonData=jsonData.substring(0,lastIndexOf);
				}
				JSONObject j = JSONObject.parseObject(jsonData);
				JSONObject json = j.getJSONObject("result");
				title = json.getString("sDocTitle");
				JSONArray array = json.getJSONArray("docinfo");
				JSONObject object = array.getJSONObject(0).getJSONObject("foolrinfo");
				content = object.getString("floorcontent");
				Document parse = Jsoup.parse(content);
				parse.select("ul").remove();
				content =parse .html();
				String dateStr ="20"+object.getString("LongDate");
				date = DateUtils.matchDate(dateStr);
				commentNum = array.size();
				author=object.getString("UserName");
			}
			if(StringUtils.isBlank(content)) {
				content = HtmlCleaner.getContentHtml(url,  doc.select(".doc_txt"));
			}
			bbs.setUpdateUrl(url);
			bbs.setTitle(title);
			bbs.setContent(content);
			bbs.setDate(date);
			bbs.setCommentNum(commentNum);
			/**
			 * 解析用于更新的地址
			 */
			bbs.setUpdateUrl(url);


			bbs.setAuthor(author);
			return bbs;
	}


	
	public NewsMeta Update(NewsMeta meta) {
		if (meta != null) {
			String updateUrl = meta.getUpdateUrl();
			if (updateUrl != null) {
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if (responseToURL != null) {
					String html = responseToURL.getHtml();
					if (html != null) {
						try {
							String jsonData = StringUtils.substringBetween(html, "var docData = ","function PreNextPageFunc(e)");
							int lastIndexOf = jsonData.lastIndexOf(";");
							if(lastIndexOf!=-1){
								jsonData=jsonData.substring(0,lastIndexOf);
							}
							JSONObject j = JSONObject.parseObject(jsonData);
							JSONObject json = j.getJSONObject("result");
							JSONArray array = json.getJSONArray("docinfo");
							Integer commentNum=array.size();
							meta.setCommentNum(commentNum);
							return meta;
						} catch (RuntimeException e) {
							e.printStackTrace();
						}

					}
				}
			}
		}
		return null;
	}

	/*
	public static void main(String[] args) throws Exception {

		XiciBbsAnalyse a = new XiciBbsAnalyse();

		String url = "http://www.xici.net/d213951967.htm#2068163";
		System.out.println(DoMainUtils.GetDomainName(url));
//		if (!a.isDetailPage(url)) {
//			System.out.println("不匹配规则");
//		} else {
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
			BbsAnalyseDiscuz.toIndex(parserHtml, meta, 2068163);
//		}

	}
	*/

	
}
